package com.ymatou.iapi.fundcommission.testcase;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.fundcommission.service.DailyReportQueryCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.database.sqlwapper.YmtFundDimensionWapper;
import com.ymttest.utils.JsonBeanHelper;
/**
*卖家后台获取佣金日账单接口
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_DailyReportQuery{
	private static DailyReportQueryCall dailyreportqueryCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("卖家后台获取佣金日账单接口");
	}

	@Before
	public void caseUp() {
		dailyreportqueryCall=new DailyReportQueryCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(dailyreportqueryCall.getOpurl());
	}

	/**
	 * 接口返回信息公用错误信息部分断言（正向Case场景）
	 * @throws Exception
	 */	
	public static void dailyreportQueryreturncode() throws Exception {
	    try {	    	
			//获取返回报文的数据
			String dataStr = dailyreportqueryCall.getReturnData();
			//返回报文断言
			Logger.verifyNotNull(dataStr, "返回报文非空");			
			String Success =  new JSONObject(dataStr).getString("Success");
			Logger.verifyIsContains(true, Success, "\"Success\":true");
			String errMsg =  new JSONObject(dataStr).getString("ErrMsg");
			Logger.verifyIsNull(errMsg, "Msg等于空");
			String errCode =  new JSONObject(dataStr).getString("ErrCode");
			Logger.verifyIsNull(errCode, "ErrCode等于空");
			String body =  new JSONObject(dataStr).getString("Body");
			Logger.verifyIsNull(body, "Body等于空");
			String requestUrl =  new JSONObject(dataStr).getString("RequestUrl");
			Logger.verifyIsNull(requestUrl, "RequestUrl等于空");
	    	    	    	
	    }catch (Exception e) {  
	        e.printStackTrace();
	    }  
	}
	
	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_DailyReportQuery_001() {
		Logger.start(true,"获取佣金总额");
		try {
			//TODO 补全case代码
			//SQL取出Ymt_CommissionCheck 表中第一条数据
			YmtFundDimensionWapper ymtFundDimensionWapper = new YmtFundDimensionWapper();
	    	Map mnull1 = new HashMap();
			List<Map> getseller = ymtFundDimensionWapper.commissiongetsellerid(mnull1);
			int sellerid= (Integer) getseller.get(0).get("SellerId");	//取出卖家id
			String sellerloginid = (String) getseller.get(0).get("SellerLoginId");	//取出卖家用户名
			String betime = (String) getseller.get(0).get("BusinessTime");	//取出业务发生时间

			Map<String,Object> aa = new HashMap<String, Object>();
			aa.put("BeginTime", betime);		
			aa.put("EndTime", betime);		
			aa.put("UserId", sellerid);	
			aa.put("UserLoginId", sellerloginid);			
			//调用接口json文件
			JSONObject dailyreportqueryBean =  JsonBeanHelper.getJsonBean("/com.ymatou.iapi.fundcommission/DailyReportQuery.json");
			dailyreportqueryBean = JsonBeanHelper.updateJsonBean(dailyreportqueryBean, aa);	
			
			dailyreportqueryCall.setData(dailyreportqueryBean);
			dailyreportqueryCall.callService();	
			
			dailyreportQueryreturncode();
			String dataStr = dailyreportqueryCall.getReturnData();	
			
			//取出费率
			Map m1 = new HashMap();
	        List<Map> trlist = ymtFundDimensionWapper.taxRateforcommission(m1);
	        BigDecimal taxRatebysql = new BigDecimal((String) trlist.get(0).get("cv")).setScale(2,BigDecimal.ROUND_HALF_UP);	//SQL查询的费率
	        double tr = new JSONObject(dataStr).getJSONArray("DailyReportList").getJSONObject(0).getDouble("TaxRate");	  //返回报文的费率
	        BigDecimal taxRate = new BigDecimal(tr).setScale(2,BigDecimal.ROUND_HALF_UP);
	        Logger.verifyEquals(taxRatebysql, taxRate, "费率对比");
			
	        //commissionchackforamount 取出费用
	        Map m2 = new HashMap();
	        m2.put("taxRatebysql", taxRatebysql);
	        m2.put("sellerid", sellerid);
	        List<Map> commissionchacklist = ymtFundDimensionWapper.commissionchackforamount(m2);
	        //收佣费用对比
	        BigDecimal subCommissionbysql = ((BigDecimal) commissionchacklist.get(0).get("SubCommission")).setScale(2, BigDecimal.ROUND_HALF_UP);//SQL查询的费率
	        double sc = new JSONObject(dataStr).getJSONArray("DailyReportList").getJSONObject(0).getDouble("SubCommission");	  //返回报文的费率
	        BigDecimal subCommission = new BigDecimal(sc).setScale(2,BigDecimal.ROUND_HALF_UP);
	        Logger.verifyEquals(subCommissionbysql, subCommission, "收佣费用对比");	        	        
	        //返佣费用对比
	        BigDecimal refundCommissionbysql = ((BigDecimal) commissionchacklist.get(0).get("RefundCommission")).setScale(2,BigDecimal.ROUND_HALF_UP);
	        double refc = new JSONObject(dataStr).getJSONArray("DailyReportList").getJSONObject(0).getDouble("RefundCommission");	  //返回报文的费率
	        BigDecimal refundCommission = new BigDecimal(refc).setScale(2,BigDecimal.ROUND_HALF_UP);
	        Logger.verifyEquals(refundCommissionbysql, refundCommission, "返佣费用对比");	        
	        //退佣费用对比
	        BigDecimal returnCommissionbysql = ((BigDecimal) commissionchacklist.get(0).get("ReturnCommission")).setScale(2,BigDecimal.ROUND_HALF_UP);
	        double retc = new JSONObject(dataStr).getJSONArray("DailyReportList").getJSONObject(0).getDouble("ReturnCommission");	  //返回报文的费率
	        BigDecimal returnCommission = new BigDecimal(retc).setScale(2,BigDecimal.ROUND_HALF_UP);
	        Logger.verifyEquals(returnCommissionbysql, returnCommission, "退佣费用对比");
	        //商品支付金额对比
	        BigDecimal realPayAmountbysql = ((BigDecimal) commissionchacklist.get(0).get("RealPayAmount")).setScale(2,BigDecimal.ROUND_HALF_UP);
	        double rpa = new JSONObject(dataStr).getJSONArray("DailyReportList").getJSONObject(0).getDouble("RealPayAmount");	  //返回报文的费率
	        BigDecimal realPayAmount = new BigDecimal(rpa).setScale(2,BigDecimal.ROUND_HALF_UP);
	        Logger.verifyEquals(realPayAmountbysql, realPayAmount, "商品支付金额对比");
	        //运费金额对比
	        BigDecimal flightAmountbysql = ((BigDecimal) commissionchacklist.get(0).get("FlightAmount")).setScale(2,BigDecimal.ROUND_HALF_UP);
	        double fa = new JSONObject(dataStr).getJSONArray("DailyReportList").getJSONObject(0).getDouble("FlightAmount");	  //返回报文的费率
	        BigDecimal flightAmount = new BigDecimal(fa).setScale(2,BigDecimal.ROUND_HALF_UP);
	        Logger.verifyEquals(flightAmountbysql, flightAmount, "运费金额对比");
	        //运预计系统返还交易服务费对比
	        BigDecimal forcastReturnbysql = ((BigDecimal) commissionchacklist.get(0).get("ForcastReturn")).setScale(2,BigDecimal.ROUND_HALF_UP);
	        double fr = new JSONObject(dataStr).getJSONArray("DailyReportList").getJSONObject(0).getDouble("ForcastReturn");	  //返回报文的费率
	        BigDecimal forcastReturn = new BigDecimal(fr).setScale(2,BigDecimal.ROUND_HALF_UP);
	        Logger.verifyEquals(forcastReturnbysql, forcastReturn, "预计系统返还交易服务费对比");
	        //预计收入对比
	        BigDecimal expIncomeWithTaxbysql = ((BigDecimal) commissionchacklist.get(0).get("ExpIncomeWithTax")).setScale(2,BigDecimal.ROUND_HALF_UP);
	        double eiwt = new JSONObject(dataStr).getJSONArray("DailyReportList").getJSONObject(0).getDouble("ExpIncomeWithTax");	  //返回报文的费率
	        BigDecimal expIncomeWithTax = new BigDecimal(eiwt).setScale(2,BigDecimal.ROUND_HALF_UP);
	        Logger.verifyEquals(expIncomeWithTaxbysql, expIncomeWithTax, "预计收入对比");
	        //应交税金对比	        
	        BigDecimal receTaxAmtbysql = ((BigDecimal) commissionchacklist.get(0).get("ReceTaxAmt")).setScale(2,BigDecimal.ROUND_HALF_UP);
	        double rta = new JSONObject(dataStr).getJSONArray("DailyReportList").getJSONObject(0).getDouble("ReceTaxAmt");	  //返回报文的费率
	        BigDecimal receTaxAmt = new BigDecimal(rta).setScale(2,BigDecimal.ROUND_HALF_UP);
	        Logger.verifyEquals(expIncomeWithTaxbysql, expIncomeWithTax, "应交税金对比");
	        //预计收入不含税金对比
	        BigDecimal rxpIncomeWithoutTaxbysql = ((BigDecimal) commissionchacklist.get(0).get("ExpIncomeWithoutTax")).setScale(2,BigDecimal.ROUND_HALF_UP);
	        double riwt = new JSONObject(dataStr).getJSONArray("DailyReportList").getJSONObject(0).getDouble("ExpIncomeWithoutTax");	  //返回报文的费率
	        BigDecimal rxpIncomeWithoutTax = new BigDecimal(riwt).setScale(2,BigDecimal.ROUND_HALF_UP);
	        Logger.verifyEquals(expIncomeWithTaxbysql, expIncomeWithTax, "预计收入不含税金对比");
	        
	        String sellerLoginIdbyresponse = (String) new JSONObject(dataStr).getJSONArray("DailyReportList").getJSONObject(0).get("SellerLoginId");
	        Logger.verifyEquals(sellerloginid, sellerLoginIdbyresponse, "用户名对比");
	        int sellerUserIdbyresponse = (Integer) new JSONObject(dataStr).getJSONArray("DailyReportList").getJSONObject(0).get("SellerUserId");
	        Logger.verifyEquals(sellerid, sellerUserIdbyresponse, "用户id对比");
	        	        
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}